package com.jml.zlhq.app.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jml.zlhq.app.R;
import com.jml.zlhq.app.AppContext;
import com.jml.zlhq.app.URLs;
import com.jml.zlhq.app.service.HttpService;
import com.jml.zlhq.app.ui.LoginActivity;
import com.jml.zlhq.app.ui.PinglunActivity;
import com.jml.zlhq.app.ui.TeamDetailActivity;
import com.jml.zlhq.app.util.BaseTools;
import com.jml.zlhq.app.util.StringUtil;
import com.jml.zlhq.app.util.UIUtil;
import com.jml.zlhq.app.widgets.RoundImageView;
import com.loopj.android.http.TextHttpResponseHandler;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MainLinGanAdapter extends IBaseAdapter {
	private List<JSONObject> itemList;
	private AppContext appContext;
	private Activity activity;
	private Fragment fragment;
	private int type;

	public MainLinGanAdapter(Activity activity, Fragment fragment, List<JSONObject> itemList, int type) {
		this.itemList = itemList;
		this.activity = activity;
		this.appContext = (AppContext) activity.getApplication();
		this.fragment = fragment;
		this.type = type;
	}

	@Override
	public int getCount() {
		return itemList == null ? 0 : itemList.size();
	}

	@Override
	public Object getItem(int position) {
		return itemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	static class ViewHolder {
		public ImageView titleIco,img;
		public TextView title,viewed,titleTag;
		public TextView name,addr,tag,nomore;
		public TextView todo,price,collect,comment;
		public LinearLayout sjLay;
	}

	@Override
	public View getView(final int position, View convertView, final ViewGroup parent) {
		
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			int layoutId = R.layout.main_lingan_item;
			convertView = LayoutInflater.from(appContext).inflate(layoutId, null);
			viewHolder.img = (ImageView) convertView.findViewById(R.id.main_lingan_item_img);
			int height = UIUtil.winWidth*320/640;
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, height);
			viewHolder.img.setLayoutParams(lp);
			viewHolder.titleIco = (RoundImageView) convertView.findViewById(R.id.main_lingan_item_title_icon);
			viewHolder.title = (TextView) convertView.findViewById(R.id.main_lingan_item_title);
			viewHolder.viewed = (TextView) convertView.findViewById(R.id.main_lingan_item_viewed);
			viewHolder.titleTag = (TextView) convertView.findViewById(R.id.main_lingan_item_type);
			viewHolder.name = (TextView) convertView.findViewById(R.id.main_lingan_item_name);
			viewHolder.addr = (TextView) convertView.findViewById(R.id.main_lingan_locat);
			viewHolder.tag = (TextView) convertView.findViewById(R.id.main_lingan_item_tag);
			viewHolder.todo = (TextView) convertView.findViewById(R.id.main_lingan_item_todo);
			viewHolder.price = (TextView) convertView.findViewById(R.id.main_lingan_item_price);
			viewHolder.collect = (TextView) convertView.findViewById(R.id.main_lingan_item_collect);
			viewHolder.comment = (TextView) convertView.findViewById(R.id.main_lingan_item_comment);
			viewHolder.nomore = (TextView) convertView.findViewById(R.id.main_lingan_item_nomore);
			viewHolder.sjLay = (LinearLayout) convertView.findViewById(R.id.main_lingan_item_sj_lay);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final JSONObject json = itemList.get(position);
		try {
			viewHolder.nomore.setVisibility((position==(itemList.size()-1)&&pageEnd)?View.VISIBLE:View.GONE);
			viewHolder.title.setText(BaseTools.getFrom("user.name", json));
			ImageLoader.getInstance().displayImage(BaseTools.getFrom("cover.image_url", json), viewHolder.img, UIUtil.getImageLoaderDisplayOptions(R.drawable.default140));
			ImageLoader.getInstance().displayImage(BaseTools.getFrom("user.logo_url", json), viewHolder.titleIco, UIUtil.getImageLoaderDisplayOptions(R.drawable.default640_320));
			viewHolder.titleTag.setText(BaseTools.getFrom("user.location", json));
			viewHolder.name.setText(BaseTools.getFrom("title", json));
			viewHolder.addr.setText(BaseTools.getFrom("place", json));
			JSONArray tags = json.get("data").toString().equals("null")?new JSONArray():json.getJSONArray("tags");
			String tagStr = "";
			for (int i = 0;tags!=null && i < tags.length(); i++) {
				tagStr += StringUtil.isEmpty(tagStr)?tags.getString(i):"&nbsp;&nbsp;"+tags.getString(i);
			}
			viewHolder.tag.setText(Html.fromHtml(tagStr));
			String privce = BaseTools.getFrom("wedding_arrangement", json);
			viewHolder.todo.setText("婚庆布置");
			viewHolder.price.setText(StringUtil.isEmpty(privce)||"0".equals(privce)?"":"￥"+BaseTools.getFrom("wedding_arrangement", json));
			viewHolder.viewed.setText(BaseTools.getFrom("statistic.CLICK", json));
			viewHolder.collect.setText(BaseTools.getFrom("statistic.COLLECT", json));
			viewHolder.comment.setText(BaseTools.getFrom("statistic.COMMENT", json));
			viewHolder.sjLay.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					String sjid = BaseTools.getFrom("user._id",json);
					String teamurl = "http://hlh.cktd.net/team/detail_web?id="+sjid;
					if(StringUtil.isEmpty(teamurl)||StringUtil.isEmpty(sjid)||sjid.equals("-1")){
						UIUtil.ToastMessage(activity, "暂无该商家信息");
					}else{
						Bundle data = new Bundle();
						data.putString("url", teamurl);
						data.putString("id", sjid);
						UIUtil.openActivity(activity, TeamDetailActivity.class, data);
					}
				}
			});
			viewHolder.comment.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(final View v) {
					Bundle bdl = new Bundle();
					bdl.putInt("type", 1);
					bdl.putString("id", BaseTools.getFrom("_id",json));
					if(type==1){
						UIUtil.openActivity(fragment, PinglunActivity.class, bdl, 2);
					}else{
						UIUtil.openActivity(activity, PinglunActivity.class, bdl, 2);
					}
				}
			});
			viewHolder.collect.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(final View v) {
					if(!appContext.isLogin()){
						UIUtil.openActivity(activity, LoginActivity.class, null,1);
					}else{
						Map<String, String> map = new HashMap<String, String>();
						map.put("auth_token", appContext.getLoginUid());
						map.put("target_id", BaseTools.getFrom("_id",json));
						final Dialog  mProDialog =  UIUtil.showLoadingDialog(activity, "提交中，请稍后...", false);
						HttpService.postByText(URLs.COLLECT_WORK, map, new TextHttpResponseHandler() {
							
							@Override
							public void onSuccess(int arg0, Header[] arg1, String arg2) {
								mProDialog.dismiss();
								try {
									JSONObject result = new JSONObject(arg2);
									if(result.getInt("status")==0){
										UIUtil.ToastMessage(activity, "收藏成功");
										try {
											int con = Integer.parseInt(((TextView)v).getText().toString());
											con++;
											itemList.get(position).getJSONObject("statistic").put("COLLECT", con);
											notifyDataSetChanged();
										} catch (Exception e) {
										}
										
									}else{
										UIUtil.ToastMessage(activity, result.getString("message"));
									}
								} catch (JSONException e) {
									e.printStackTrace();
									UIUtil.ToastMessage(activity, "收藏失败");
								}
							}
							
							@Override
							public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
								mProDialog.dismiss();
								UIUtil.ToastMessage(activity, "收藏失败");
							}
						});
					}
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return convertView;
	}

	
	
	

}
